﻿using PBO.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PBO.Game.BattleClass
{
    public partial class Pokemon
    {
        public int MaxHp { get; private set; }

        public int HealthPower { get; private set; }

        public bool IsFullHp
        {
            get { return MaxHp == HealthPower; }
        }

        #region hp

        /// <summary>
        /// HPRecover Max Hp / denominator
        /// </summary>
        public void HPRecoverFraction(int denominator)
        {
            double hp = (double)this.MaxHp / (double)denominator;
            HPRecover(hp);
        }

        /// <summary>
        /// HP回复
        /// </summary>
        /// <param name="recovery"></param>
        public void HPRecover(double recovery)
        {
            int recover = (int)Math.Max(1, Math.Truncate(recovery));
            SetHP(this.HealthPower + recover);
        }

        public bool RecoverPokemonHP(int recovery)
        {
            if (!this.CheckIfHPRecoveryIsEnabled())
            {
                return false;
            }
            if (this.HealthPower < this.MaxHp)
            {
                this.HPRecover(recovery);
                MyTeam.log.AppendMsg("HpRecover", this.CustomName);
                //log.AddText(pm.CustomName + "回复了HP！");
                return true;
            }
            else
            {
                MyTeam.log.AppendMsg("FullHp", this.CustomName);
                //log.AppendText(pm.CustomName + "的HP已经满了");
                return false;
            }
        }

        /// <summary>
        /// 回复禁止状态
        /// </summary>
        /// <returns></returns>
        public bool CheckIfHPRecoveryIsEnabled()
        {
            if (BattleState.healBlock)
            {
                MyTeam.log.AppendMsg("EnHealBlock", this.GetNameString());
                //MyTeam.log.AddText(GetNameString() + "被禁止了回复");
            }
            return !BattleState.healBlock;
        }

        public bool SetHP(int value)
        {
            int hp = Math.Max(0, Math.Min(MaxHp, value));
            bool result = this.HealthPower != hp;
            if (result)
            {
                this.HealthPower = hp;
                OnHpChanged();
            }
            return result;
        }

        #endregion

        /// <summary>
        /// Hurt Max Hp / denominator
        /// </summary>
        public void HurtFraction(int denominator)
        {
            double hp = (double)this.MaxHp / (double)denominator;
            GetHurt(hp);
        }

        /// <summary>
        /// 伤害实现
        /// </summary>
        public void GetHurt(double damageValue)
        {
            if (damageValue > 0)
            {
                int damage = Convert.ToInt32(Math.Truncate(damageValue));

                damage = Math.Max(1, damage);

                SetHP(Math.Max(0, this.HealthPower - damage));

                if (this.HealthPower == 0)
                {
                    State = PokemonState.No;
                    MyTeam.log.AppendMsg("Faint", this.GetNameString());
                    //MyTeam.log.AddText(GetNameString() + "倒下了！");
                    OnDied();
                }
                else
                {
                    if (GetItem() == Item.OranBerry || (GetItem() >= Item.SitrusBerry && GetItem() <= Item.IapapaBerry))
                    {
                        RaiseItem();
                    }
                    else if ((GetItem() >= Item.LiechiBerry && GetItem() <= Item.StarfBerry) || GetItem() == Item.MicleBerry)
                    {
                        RaiseItem();
                    }
                }
            }
        }

        public void SubGetHurt(int damageValue)
        {
            BattleState.subHP -= damageValue;
            if (BattleState.subHP <= 0)
            {
                BattleState.substituted = false;
                OnImageChanged();
            }
        }
    }
}
